package com.syt.cellphone.information.ui.search

import android.os.Bundle
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.syt.cellphone.information.R
import com.syt.cellphone.information.base.VMBaseActivity
import com.syt.cellphone.information.data.pojo.PhoneBase
import com.syt.cellphone.information.lib.theme.ATH
import com.syt.cellphone.information.lib.theme.primaryTextColor
import com.syt.cellphone.information.ui.main.phone.groups.BasePhoneAdapter
import com.syt.cellphone.information.ui.main.phone.groups.PhoneBaseAdapterList
import com.syt.cellphone.information.ui.phone.details.PhoneDetailsActivity
import com.syt.cellphone.information.utils.getViewModel
import kotlinx.android.synthetic.main.activity_search.*
import org.jetbrains.anko.startActivity

/**
 * @author       zhulei
 * @data         2021/1/18 10:42
 * @function    搜索
 */
class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search) {

    override val viewModel: SearchViewModel
        get() = getViewModel(SearchViewModel::class.java)
    private lateinit var basePhoneAdapter: BasePhoneAdapter

    private lateinit var searchView: SearchView


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        searchView = findViewById(R.id.search_view)
        initSearchView()
        initRv()
    }

    private fun initSearchView() {
        ATH.setTint(searchView, primaryTextColor)
        // 展开
        searchView.onActionViewExpanded()
        // 启动提交按钮
        searchView.isSubmitButtonEnabled = true
        searchView.queryHint = getString(R.string.search_phone)
        searchView.clearFocus()
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                searchView.clearFocus()
                query?.let {
                    viewModel.search(it)
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return false
            }
        })
    }

    private fun initRv() {
        ATH.applyEdgeEffectColor(rv_search_view)
        // 下拉刷新，暂时算全部刷新吧
        refresh_layout.setOnRefreshListener {
            it.finishRefresh(3000)
            viewModel.search(searchView.query.toString())
        }
        // 上拉刷新
        refresh_layout.setOnLoadMoreListener {
            it.finishLoadMore(3000)
            viewModel.search(searchView.query.toString(), false)
        }
        rv_search_view.layoutManager = LinearLayoutManager(this)
        basePhoneAdapter = PhoneBaseAdapterList(this, object : BasePhoneAdapter.CallBack {
            override fun openPhoneInfo(base: PhoneBase) {
                // 打开详情Activity
                startActivity<PhoneDetailsActivity>(Pair("phoneId", base.baseId.toString()))
            }
        })
        rv_search_view.adapter = basePhoneAdapter
        viewModel.searchLiveData.observe(this, {
            basePhoneAdapter.setItems(it)
            refresh_layout.finishLoadMore()
            refresh_layout.finishRefresh()
        })
    }

}